//
//  ETH+Balance.swift
//  XHWalletUtils
//
//  Created by xinph on 2020/5/16.
//  Copyright © 2020 xinph. All rights reserved.
//

import Foundation

extension ETH: BalanceProtocol {
    public func getETHBalance(_ address: AddressConvertible, closure: @escaping (Result<String>) -> Void) {
        guard !Config.default.accessToken.isEmpty else {
            closure(Result.failure(XHError(addressNotFound)))
            print(addressNotFound)
            return
        }
        guard let web3 = self.generateWeb3() else { return }
        web3.eth.getBalancePromise(address: address.asAddress())
            .done({ (balanceResult) in
                let balanceString = Web3.Utils.formatToEthereumUnits(balanceResult, toUnits: .wei, decimals: 0)!
                closure(Result.success(balanceString))
            }).catch({ (err) in
                closure(Result.failure(XHError(err: err)))
                print(err)
            }).finally {
                //关闭菊花
        }
    }
}
